from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium import webdriver
import time
TIMEOUT = 8

class Base:

    # 封装驱动
    def __init__(self, driver):
        self.driver = driver

    # 打开网址
    def goto(self, url):
        self.driver.get(url)

    # 显示等待元素点击
    def wait_click(self,loc,timeout=None) -> WebElement:
        timeout = TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver,timeout=timeout)
        return wait.until(ec.element_to_be_clickable(loc))

    # 显示等待文本
    def wait_text(self, loc, timeout=None)->WebElement:
        timeout = TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver,timeout=timeout)
        return wait.until(ec.visibility_of_element_located(loc))

    # 点击
    def click(self, loc):
        self.wait_click(loc).click()

    # 输入
    def send_keys(self,loc,text):
        self.wait_click(loc).send_keys(text)

    # 文本
    def text(self,loc):
        return self.wait_text(loc).text

    # 强制等待
    def sleep(self, timeout):
        time.sleep(int(timeout))

    # def open_browser(self, browser_type='Chrome'):
    #     if browser_type.lower() == 'chrome':
    #         self.driver = webdriver.Chrome()
    #     elif browser_type.lower() == 'edge':
    #     self.driver = webdriver.Edge()
    #     self.driver.maximize_window()
    #
    # # 在指定的输入框中输入文本
    # def input_text(self,loc,text):
    #     ele = self.driver.find_element(*loc)
    #     ele.send_keys(text)
    # # 清空内容
    # def clear(self,loc):
    #     ele = self.driver.find_element(*loc)
    #     ele.clear()